﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PowerView.Page
{
    public partial class Wave_Form : Form
    {
        Thread wave = null;
        Pen[] wave_pens = new Pen[6];
        CheckBox[] wave_check = new CheckBox[6];

        Pen Back_Pen = new Pen(Color.Gray, 1);

        bool Run_State = false;
        int stop_point = 0;
        public Wave_Form()
        {
            InitializeComponent();
            Back_Pen.DashPattern = new float[] { 5, 3 };

            wave_pens[0] = new Pen(Vbus_checkBox.BackColor, 1);
            wave_pens[1] = new Pen(Ibus_checkBox.BackColor, 1);
            wave_pens[2] = new Pen(Dp_checkBox.BackColor, 1);
            wave_pens[3] = new Pen(Dn_checkBox.BackColor, 1);
            wave_pens[4] = new Pen(CC1_checkBox.BackColor, 1);
            wave_pens[5] = new Pen(CC2_checkBox.BackColor, 1);

            wave_check[0] = Vbus_checkBox;
            wave_check[1] = Ibus_checkBox;
            wave_check[2] = Dp_checkBox;
            wave_check[3] = Dn_checkBox;
            wave_check[4] = CC1_checkBox;
            wave_check[5] = CC2_checkBox;
        }

        void Wave_Refresh()
        {
            Bitmap image = new System.Drawing.Bitmap(Wave_pBox.Width, Wave_pBox.Height);
            Graphics g = Graphics.FromImage(image);
            Font drawFont = new Font("Arial", 10);
            SolidBrush drawBrush = new SolidBrush(Color.White);
            while (true)
            {
                if(image.Width != Wave_pBox.Width || image.Height != Wave_pBox.Height) // 窗体大小发生变化
                {
                    g.Dispose();
                    image.Dispose();
                    image = new Bitmap(Wave_pBox.Width, Wave_pBox.Height);
                    g = Graphics.FromImage(image);
                }
                g.Clear(Color.Black);

                int y_interval = (Wave_pBox.Height - 15) / 7; // 上边距10， 下边距5
                int h_div = 0;
                for(int y = Wave_pBox.Height - 5; y > 0; y -= y_interval) //绘制水平网格线
                {
                    g.DrawLine(Back_Pen, 5, y, Wave_pBox.Width - 5, y);
                    g.DrawString((h_div*3) + "V", drawFont, drawBrush, 0, y - 12);
                    g.DrawString((h_div) + "V(A)", drawFont, drawBrush, Wave_pBox.Width - 40, y - 12);
                    h_div ++;
                }

                for (int x = Wave_pBox.Width - 5; x > 0; x -= 100) // 绘制竖直网格线
                {
                    g.DrawLine(Back_Pen, x, 10, x, Wave_pBox.Height - 5);
                }

                int wave_start = 0;
                if (Run_State == true) // Run 状态
                {
                    wave_start = MiddleWare.Wave_Buff_Index - 1;
                    if (wave_start == -1) wave_start = MiddleWare.Wave_Buff_Len - 1;
                }
                else // Stop 状态
                {
                    wave_start = stop_point;
                }

                int[] last_y = new int[6]{ 0, 0, 0, 0, 0, 0 };
                double[] title_volt = new double[6];
                double[] wave_ratio = new double[6] { 8.8, 6.6, 3.3, 3.3, 3.3, 3.3 };
                double[] volt_ratio = new double[6] {26.4, 6.6, 3.3, 3.3, 3.3, 3.3 };
                for (int i = 0; i < 6; i++)
                {
                    last_y[i] = MiddleWare.wave_buff[wave_start, 109 + i * 2] * 256 + MiddleWare.wave_buff[wave_start, 108 + i * 2];
                    title_volt[i] = last_y[i] * volt_ratio[i] / 4096;
                    last_y[i] = (int)(last_y[i] * wave_ratio[i] * y_interval / 4096);
                    last_y[i] = Wave_pBox.Height - 5 - last_y[i];
                }

                for (int x = Wave_pBox.Width - 5; x > 5;) // 绘制波形
                {
                    for (int wave_i = 109; wave_i >= 0; wave_i -= 12)
                    {
                        for (int i = 0; i < 6; i++)
                        {
                            if (wave_check[i].Checked == false) continue;
                            int y = MiddleWare.wave_buff[wave_start, wave_i + i * 2] * 256 + MiddleWare.wave_buff[wave_start, wave_i + i * 2 - 1];
                            y = (int)(y * wave_ratio[i] * y_interval / 4096);
                            y = Wave_pBox.Height - 5 - y;
                            g.DrawLine(wave_pens[i], x, last_y[i], x - 1, y);
                            last_y[i] = y;
                        }
                        x--;
                    }
                    wave_start--;
                    if (wave_start == -1) wave_start = MiddleWare.Wave_Buff_Len - 1;
                }
                Vbus_checkBox.Text = "VBus : " + title_volt[0].ToString("0.00");
                Ibus_checkBox.Text = "Ibus : " + title_volt[1].ToString("0.00");
                Dp_checkBox.Text = "D+ : " + title_volt[2].ToString("0.00");
                Dn_checkBox.Text = "D- : " + title_volt[3].ToString("0.00");
                CC1_checkBox.Text = "CC1 : " + title_volt[4].ToString("0.00");
                CC2_checkBox.Text = "CC2 : " + title_volt[5].ToString("0.00");
                Wave_pBox.CreateGraphics().DrawImage(image, 0, 0); // 刷新图片
                Thread.Sleep(30);
            }
        }

        private void Wave_Form_Load(object sender, EventArgs e)
        {
            wave = new Thread(Wave_Refresh);
            wave.IsBackground = true;
            wave.Start();
        }

        private void Wave_Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            wave.Abort();
        }

        public void Wave_Run()
        {
            if (MiddleWare.Write_CMD(0xFF, 0x00, 0x01, new byte[] { 0x01 }, 1) != 0)
            {
                return;
            }
            Run_State = true;
            Run_Btn.Text = "RUN(F12)";
            Run_Btn.BackColor = Color.Lime;
            Wave_ScrollBar.Enabled = false;
        }

        public void Wave_Stop()
        {
            if (MiddleWare.Write_CMD(0xFF, 0x00, 0x01, new byte[] { 0x00 }, 1) != 0)
            {
                return;
            }
            Run_State = false;
            stop_point = MiddleWare.Wave_Buff_Index - 1;
            if (stop_point == -1) stop_point = MiddleWare.Wave_Buff_Len - 1;
            Run_Btn.Text = "STOP(F12)";
            Run_Btn.BackColor = Color.Red;
            Wave_ScrollBar.Enabled = true;
            Wave_ScrollBar.Value = 1000;
        }
        public void Run_Btn_Click(object sender, EventArgs e)
        {
            if (Run_Btn.Text.StartsWith("STOP"))
            {
                Wave_Run();
            }
            else
            {
                Wave_Stop();
            }
        }

        private void Wave_ScrollBar_Scroll(object sender, ScrollEventArgs e)
        {
            int tmp = (int)(MiddleWare.Wave_Buff_Len * ((double)Wave_ScrollBar.Value / 1000));

            if ((tmp + MiddleWare.Wave_Buff_Index) > MiddleWare.Wave_Buff_Len)
            {
                stop_point = tmp + MiddleWare.Wave_Buff_Index - MiddleWare.Wave_Buff_Len;
            }
            else
            {
                stop_point = tmp + MiddleWare.Wave_Buff_Index;
            }
        }
    }
}
